import logging
import re

from django import http
from django.http import HttpResponse
from django.shortcuts import render, redirect, reverse

# Create your views here.
from django.views import View
from django.http.request import HttpRequest

from apps.users.models import User

logger = logging.getLogger('django')
class RegisterView(View):
    #必须要和后端交互的是
    #1 用户名是否重复/手机号是否重复
    #2 图形验证码
    #3 手机验证码
    #4 注册功能
    def get(self, request):
        return render(request, 'register.html')

    #注册功能
    def post(self, request):
        print('dddd')
        """
        1. 接受前端提交的用户名,密码和手机号
        2. 数据验证
            2.1验正码的比对
            2.2 判断用户名
            2.3 判断密码是否符合规则
            2.4 密码和确认密码是否一致
            2.5 判断手机号是否符合规则
        3. 数据没有问题才入库
        4. 返回相应
        """
    #     1. 接受前端提交的用户名, 密码和手机号
        data = request.POST
        username = data.get('username')
        passworld = data.get('password')
        passworld2 = data.get('password2')
        mobile = data.get('mobile')
        allow = data.get('allow')
    #     2.
    #     数据验证
    #     2.1 验正码的比对
        if not all([username, passworld, passworld2, mobile]):
            return http.HttpResponseBadRequest('参数有问题')
    #     2.2判断用户名
        if not re.match(r'[0-9a-zA-Z_]{5,20}', username):
            return http.HttpResponseBadRequest('用户名不合法')
    #     2.3判断密码是否符合规则
        if not re.match(r'[0-9a-zA-Z_]', passworld):
            return http.HttpResponseBadRequest('密码不合法')

    #     2.4密码和确认密码是否一致
        if passworld != passworld2:
            return http.HttpResponseBadRequest('密码不一致')
    #     2.5 判断手机号是否符合规则
        if not re.match(r'1[3-9]\d{9}', mobile):
            return http.HttpResponseBadRequest('手机号不符合规则')
    #     2.6 是否勾选协议
        if not allow == 'on':
            return http.HttpResponseBadRequest('请勾选用户协议')
    # 3.  数据没有问题才入库
        #当我们进行操作外界资源的时候(mysql, file, redis),要进行异常处理
        try:
            user = User.objects.create_user(username=username, password=passworld, mobile=mobile)
        except Exception as e:
            logger.error(e)
            #或者是返回错误页面
           # return render(request, 'register.html', context={'error_message':'数据库异常错误'})
            return http.HttpResponseBadRequest('数据库异常错误')


    # 4. 返回相应
        #注册成功之后默认就是登录状态
        #保持登录状态
        #request.session
        from django.contrib.auth import login
        #可以使用系统帮我们实现的
        login(request, user)
        return redirect(reverse('contents:index'))


class UsernameCountView(View):

    def get(self, request,username):

        #凡是与外界操作相关的都加上异常捕获
        try:
           count = User.objects.filter(username=username).count()
        except Exception as e:
            print(e)
            return http.JsonResponse({'code': 400, 'errmsg': '数据库异常'})

        return http.JsonResponse({'code': 0 ,'count': count})

class MobileCountView(View):

    def get(self, request, mobile):

        try:
            count = User.objects.filter(mobile=mobile).count()
        except Exception as e:
            return http.JsonResponse({"code": 400, "errmsg": '数据库异常'})

        return http.JsonResponse({"code": 0, 'count': count})